package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Student;
import com.example.demo.entity.User;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.AjaxJson;
import com.example.demo.vo.studentAddVo;
import com.example.demo.vo.studentSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 学生信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-11-10
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<studentSelectVo> queryList() {
        List<Student> list = this.list();
        return list.stream().map(this::getUser).toList();
    }

    @Override
    public studentSelectVo queryById(Integer id) {
        Student student = this.getById(id);
        return getUser(student);
    }

    @Override
    public Student getByUserId(Integer userId) {
        return this.getOne(new QueryWrapper<Student>().eq("user_id", userId));
    }

//    @Override
//    public AjaxJson add(studentAddVo student,Integer userId) {
//        Student student1 = new Student();
//        student1.setUserId(student.getUserId());
//        student1.setClassId(student.getClassId());
//
//    }

    private studentSelectVo getUser(Student student) {
        studentSelectVo studentSelectVo = new studentSelectVo();
        studentSelectVo.setStudentId(student.getStudentId());
        studentSelectVo.setClassId(student.getClassId());
        studentSelectVo.setStudentNo(student.getStudentNo());
        studentSelectVo.setEnrollmentDate(student.getEnrollmentDate());
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", student.getUserId()));
        studentSelectVo.setUsername(user.getUsername());
        studentSelectVo.setRealName(user.getRealName());
        studentSelectVo.setGender(user.getGender());
        studentSelectVo.setPhone(user.getPhone());
        studentSelectVo.setEmail(user.getEmail());
        studentSelectVo.setCreateTime(user.getCreateTime());
        return studentSelectVo;
    }
}
